package com.red.servlet;

import com.red.entity.User;
import com.red.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new UserServiceImpl().selectByAC(username, password);

        if (user != null) {
            req.getSession().setAttribute("user",user);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.sendRedirect("index.jsp");
        } else {
            resp.getWriter().print("账号或密码错误");
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
}
